//
//  LeftSortViewController.m
//  Blank
//
//  Created by 马博 on 15/9/24.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "LeftSortViewController.h"
#import "AppDelegate.h"
#import "CollectViewController.h"
#import "CalculateFileSize.h"
#import "Our_ViewController.h"


@interface LeftSortViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic , assign)BOOL nightCount;

@end

@implementation LeftSortViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    UIImageView *imageview = [[UIImageView alloc] initWithFrame:self.view.bounds];
    imageview.image = [UIImage imageNamed:@"NewsBackground"];
    [self.view addSubview:imageview];
    
    UITableView *tableview = [[UITableView alloc] init];
    self.tableview = tableview;
    tableview.frame = self.view.bounds;
    tableview.dataSource = self;
    tableview.delegate  = self;
    tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:tableview];
    
    
}

//section数量

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 4;
}


//每行cell是什么
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *Identifier = @"Identifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Identifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Identifier];
    }
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.textLabel.font = [UIFont systemFontOfSize:20.0f];
    cell.backgroundColor = [UIColor clearColor];
    cell.textLabel.textColor = [UIColor whiteColor];
    
    if (indexPath.row == 0) {
        
        NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"我的收藏"];

        [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Marker Felt" size:20] range:NSMakeRange(0, 4)];
        
        cell.textLabel.attributedText = str;
        
    } else if (indexPath.row == 1) {
        cell.textLabel.text = @"夜间模式";
    } else if (indexPath.row == 2) {
        cell.textLabel.text = @"清理缓存";
    } else if (indexPath.row == 3) {
        cell.textLabel.text = @"关于我们";
    }
    
    return cell;
}


//选中cell之后的效果

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    CollectViewController *col = [[CollectViewController alloc] init];
    [tempAppDelegate.LeftSlideVC closeLeftView];//关闭左侧抽屉
    NSArray *arr = tempAppDelegate.tb.viewControllers;
    UINavigationController *vc = arr[tempAppDelegate.pageCount];
        
    [vc pushViewController:col animated:YES];
        
    }
    
    else if (indexPath.row == 1){
        self.nightCount = [NightModel getColorModel];
        
        if (self.nightCount == 0) {
            
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"打开夜间模式?" preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                self.nightCount = 1;
                [NightModel changeNightModel:self.nightCount];
                [alert dismissViewControllerAnimated:YES completion:^{
                    
                }];
                [[NSNotificationCenter defaultCenter] postNotificationName:@"nightModelChange" object:nil];
            }];
            UIAlertAction *actionCancle = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                [alert dismissViewControllerAnimated:YES completion:^{
                    
                }];
            }];
            
            [alert addAction:action];
            [alert addAction:actionCancle];
            
            [self presentViewController:alert animated:YES completion:^{
                
            }];
        }
        else{
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"关闭夜间模式?" preferredStyle:UIAlertControllerStyleAlert];
        
            UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                self.nightCount = 0;
                [NightModel changeNightModel:self.nightCount];
                [alert dismissViewControllerAnimated:YES completion:^{
                    
                }];
                [[NSNotificationCenter defaultCenter] postNotificationName:@"nightModelChange" object:nil];
            }];
            UIAlertAction *actionCancle = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                [alert dismissViewControllerAnimated:YES completion:^{
                    
                }];
            }];
            
            [alert addAction:action];
            [alert addAction:actionCancle];
            
            
            [self presentViewController:alert animated:YES completion:^{
                
            }];
        }
    }
    
    else if(indexPath.row == 2){
    
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            NSString *cachPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
            NSLog(@"%@", cachPath);
            NSArray *files = [[NSFileManager defaultManager] subpathsAtPath:cachPath];
            [self performSelectorOnMainThread:@selector(clearCachesSuccess) withObject:nil waitUntilDone:YES];
            for (NSString *p in files) {
                NSError *error;
                NSString *path = [cachPath stringByAppendingPathComponent:p];
                if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
                    [[NSFileManager defaultManager] removeItemAtPath:path error:&error];
                }
            }
     });
    
    }
    else{
        
        NSLog(@"关于我们");
        
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
        
        AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        Our_ViewController *our = [[Our_ViewController alloc] init];
        
        [tempAppDelegate.LeftSlideVC closeLeftView];//关闭左侧抽屉
        
        [tempAppDelegate.mainNavigationController pushViewController:our animated:NO];
        
    }
    
}

- (void)clearCachesSuccess
{
    NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    /* 清除缓存大小 */
    float count = [[CalculateFileSize defaultCalculateFileSize] folderSizeAtPath:cachePath];
    NSString *str = [NSString stringWithFormat:@"成功清理%.2fMB缓存", count];
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:str delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alertView show];
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 180;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableview.bounds.size.width, 180)];
    view.backgroundColor = [UIColor clearColor];
    return view;
}


-(void)dismissPresented:(id)sender {
    [self.presentedViewController dismissViewControllerAnimated:YES completion:nil];
}


#pragma mark - ReceiveMemory
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
